package one.stand.service.impl;

import com.google.common.collect.Lists;
import com.jmp.base.dto.GoodsGroupBuyingDto;
import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.dto.GoodsDetailDto;
import one.stand.dto.GoodsTypeDto;
import one.stand.mapper.GoodSpecMapperExt;
import one.stand.mapper.GoodsMapperExt;
import one.stand.mapper.GoodsMapperExtPublic;
import one.stand.model.GoodSpec;
import one.stand.model.Goods;
import one.stand.service.GoodsBaseService;
import one.stand.util.AssertUtil;
import one.stand.util.OneListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author cjr
 */
@Service
@Slf4j
public class GoodsBaseServiceImpl implements GoodsBaseService {
    private static Pattern pattern = Pattern.compile("(>(.+?)</p>)|(<img.+?src=\"(.+?)\".+?>)");

    @Autowired
    private GoodsMapperExtPublic goodsMapperExtPublic;
    @Autowired
    private GoodsMapperExt goodsMapperExt;
    @Autowired
    private GoodSpecMapperExt goodSpecMapperExt;

    @Override
    public Goods get(Integer goodsId) {
        return goodsMapperExt.selectByPrimaryKey(goodsId);
    }

    @Override
    public Goods getOn(Integer goodsId) {
        Goods goodsModel = goodsMapperExt.selectByPrimaryKey(goodsId);
        return Objects.nonNull(goodsModel) && goodsModel.getIsDel() == 0 ? goodsModel : null;
    }

    @Override
    public GoodSpec getSpec(Integer specId) {
        return goodSpecMapperExt.selectByPrimaryKey(specId);
    }

    @Override
    public List<GoodSpec> getSpecs(Integer goodsId) {
        return goodSpecMapperExt.selectSpecs(goodsId);
    }

    @Override
    public List<GoodsTypeDto> getTypes(Integer goodsId) {
        return goodsMapperExtPublic.selectTypes(goodsId);
    }

    @Override
    public boolean hasAr(Integer goodsId) {
        return goodSpecMapperExt.selectArCount(goodsId) > 0;
    }

    @Override
    public void deleteByCompanyId(Integer companyId) {
        AssertUtil.nonNull(companyId, ResultEnum.PARAM_CHECK);
        goodsMapperExtPublic.deleteByCompanyId(companyId);
    }

    @Override
    public String getDetails(GoodsDetailDto goodsDetailDto) {
        String detail = StringUtils.isNoneBlank(goodsDetailDto.getGoodsDetailsMemo()) ? "<p class=\"bgfff pt15 pl15 pr15 pb15 fs14 ca8 borderbox w100p\">" + goodsDetailDto.getGoodsDetailsMemo() + "</p>" : StringUtils.EMPTY;
        if (OneListUtil.isNotEmpty(goodsDetailDto.getGoodsDetailsImgs())) {
            detail = detail + goodsDetailDto.getGoodsDetailsImgs().stream()
                    .filter(str -> StringUtils.isNoneBlank(str) && !"null".equals(str))
                    .map(str -> "<img src=\"" + str + "\" alt=\"\" class=\"w100p\" style=\"display: flex;\" mode=\"widthFix\"></img>")
                    .collect(Collectors.joining());
        }
        return detail;
    }

    @Override
    public GoodsDetailDto getDetails(String detail) {
        boolean goodsDetailsEditFlag = true;
        String goodsDetailsMemo = null;
        List<String> goodsDetailsImgs = null;
        if (StringUtils.isNoneBlank(detail)) {
            goodsDetailsImgs = Lists.newArrayList();
            int i = 0;
            Matcher m = pattern.matcher(detail);
            while (m.find()) {
                if (i == 0) {
                    goodsDetailsMemo = m.group(2);
                    if (StringUtils.isNoneBlank(m.group(4))) {
                        goodsDetailsImgs.add(m.group(4));
                    }
                } else if (i > 0 && StringUtils.isNoneBlank(m.group(2))) {
                    goodsDetailsEditFlag = false;
                    continue;
                } else if (StringUtils.isNoneBlank(m.group(4))) {
                    goodsDetailsImgs.add(m.group(4));
                }
                i++;
            }
            goodsDetailsEditFlag = goodsDetailsEditFlag && (StringUtils.isNoneBlank(goodsDetailsMemo) || OneListUtil.isNotEmpty(goodsDetailsImgs));
        }

        return GoodsDetailDto.builder()
                .goodsDetailsEditFlag(goodsDetailsEditFlag)
                .goodsDetailsMemo(goodsDetailsMemo)
                .goodsDetailsImgs(goodsDetailsImgs)
                .build();
    }

    @Override
    public GoodsGroupBuyingDto getGroup(Integer groupId) {
        GoodsGroupBuyingDto goodsGroupBuyingDto = goodsMapperExtPublic.selectGroup(groupId);
        return goodsGroupBuyingDto;
    }

    @Override
    public String getGroupShareH5Url(Integer cardId, Integer groupId, String groupPrice) {
        return String.format("http://hcard.hongmushichang.com/pintuan/index.html?cardId=%s&groupId=%s&price=%s", cardId, groupId, groupPrice);
    }
}
